public class O26 {
    public static void main(String[] args) {
        O26 o = new O26();
        TreeNode t = new TreeNode(1);
        t.left = new TreeNode(2);
        t.right = new TreeNode(3);
        System.out.println(o.isSubStructure(t,new TreeNode(2)));
    }
    TreeNode AA;
    boolean flag = false;
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B==null||A==null)
            return false;
        ser(A,B);
        if (AA==null)
            return false;
        else A=AA;
        System.out.println(A.val);
        System.out.println(B.val);
        return dfs(A,B);
    }
    boolean dfs(TreeNode A, TreeNode B){
        if (B==null)
            return true;
        if (A==null)
            return false;
        return A.val==B.val&&dfs(A.left,B.left)&&dfs(A.right,B.right);
    }
    void ser(TreeNode A, TreeNode B){
        if (flag)
            return;
        if (A.val==B.val){
            AA = A;
            flag = true;
        }
        if (A.left!=null)
            ser(A.left,B);
        if (A.right!=null)
            ser(A.right,B);
    }
}
